package com.canaan.business.service.inOutStock.impl;

import com.canaan.business.domain.inOutStock.WmsPickingRecord;
import com.canaan.business.mapper.inOutStock.WmsPickingRecordMapper;
import com.canaan.business.service.inOutStock.IWmsPickingRecordService;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.StringUtils;
import com.canaan.common.utils.TableNoUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 拣货记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-12-20
 */
@Service
public class WmsPickingRecordServiceImpl implements IWmsPickingRecordService
{
    @Resource
    private WmsPickingRecordMapper wmsPickingRecordMapper;

    /**
     * 查询拣货记录
     *
     * @param id 拣货记录主键
     * @return 拣货记录
     */
    @Override
    public WmsPickingRecord selectWmsPickingRecordById(Long id)
    {
        return wmsPickingRecordMapper.selectWmsPickingRecordById(id);
    }

    /**
     * 查询拣货记录列表
     *
     * @param wmsPickingRecord 拣货记录
     * @return 拣货记录
     */
    @Override
    public List<WmsPickingRecord> selectWmsPickingRecordList(WmsPickingRecord wmsPickingRecord)
    {
        return wmsPickingRecordMapper.selectWmsPickingRecordList(wmsPickingRecord);
    }

    /**
     * 新增拣货记录
     *
     * @param wmsPickingRecord 拣货记录
     * @return 结果
     */
    @Override
    public int insertWmsPickingRecord(WmsPickingRecord wmsPickingRecord)
    {
        wmsPickingRecord.setCreateTime(DateUtils.getNowDate());
        return wmsPickingRecordMapper.insertWmsPickingRecord(wmsPickingRecord);
    }
    /**
     * 批量新增拣货任务单
     *
     * @param pickingRecordList 拣货任务单列表
     * @return 结果
     */
    public int batchInsertWmsPickingRecord(List<WmsPickingRecord> pickingRecordList){
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (WmsPickingRecord pickingRecord : pickingRecordList){
            if(StringUtils.isBlank(pickingRecord.getNo())){
                pickingRecord.setNo(TableNoUtil.getNo(WmsPickingRecord.class));
            }
            pickingRecord.setCreateBy(username);
            pickingRecord.setCreateTime(currerTime);
        }
        int insertNum = 0;
            for (int i=0; i<pickingRecordList.size();) {
            int endIndex = i+500;
            if (endIndex > pickingRecordList.size()) {
                endIndex = pickingRecordList.size();
            }
            insertNum = insertNum + wmsPickingRecordMapper.batchInsertWmsPickingRecord(pickingRecordList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }


    /**
     * 修改拣货记录
     *
     * @param wmsPickingRecord 拣货记录
     * @return 结果
     */
    @Override
    public int updateWmsPickingRecord(WmsPickingRecord wmsPickingRecord)
    {
        wmsPickingRecord.setUpdateTime(DateUtils.getNowDate());
        return wmsPickingRecordMapper.updateWmsPickingRecord(wmsPickingRecord);
    }

    /**
     * 批量删除拣货记录
     *
     * @param ids 需要删除的拣货记录主键
     * @return 结果
     */
    @Override
    public int deleteWmsPickingRecordByIds(Long[] ids)
    {
        return wmsPickingRecordMapper.deleteWmsPickingRecordByIds(ids);
    }

    /**
     * 删除拣货记录信息
     *
     * @param id 拣货记录主键
     * @return 结果
     */
    @Override
    public int deleteWmsPickingRecordById(Long id)
    {
        return wmsPickingRecordMapper.deleteWmsPickingRecordById(id);
    }
}
